package com.copm.ifm.servers.sys.service.impl.process;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.copm.ifm.base.api.pojo.PageDTO;
import com.copm.ifm.base.api.pojo.PageVO;
import com.copm.ifm.base.basic.ServiceResponse;
import com.copm.ifm.base.basic.constant.DbConstants;
import com.copm.ifm.base.basic.enums.ResponseCodeEnum;
import com.copm.ifm.base.basic.exception.ServiceException;
import com.copm.ifm.base.basic.util.AuthUtils;
import com.copm.ifm.base.service.mp.page.PageProxy;
import com.copm.ifm.servers.sys.dto.SysTokenDTO;
import com.copm.ifm.servers.sys.entity.SysStaff;
import com.copm.ifm.servers.sys.entity.SysToken;
import com.copm.ifm.servers.sys.mapper.process.SysTokenMapper;
import com.copm.ifm.servers.sys.mapper.staff.SysStaffMapper;
import com.copm.ifm.servers.sys.service.process.SysTokenService;
import com.copm.ifm.servers.sys.vo.SysTokenVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 固定token表 服务实现类
 *
 * @author tp
 * @since 2021-01-21
 */
@Service
public class SysTokenServiceImpl extends ServiceImpl<SysTokenMapper, SysToken> implements SysTokenService {

    @Resource
    SysTokenMapper sysTokenMapper;

    @Resource
    SysStaffMapper sysStaffMapper;

    @Override
    public PageVO<SysTokenVO> selectForPage(PageDTO<SysTokenDTO> pageDTO) {
        PageVO<SysTokenVO> proxy = PageProxy.proxy(baseMapper::selectForPage, pageDTO);
        List<SysTokenVO> records = proxy.getRecords();
        ServiceResponse<List<SysTokenVO>> success = new ServiceResponse<>(records, ResponseCodeEnum.SUCCESS);
        proxy.setRecords(success.getData());
        return proxy;
    }

    @Override
    public void add(SysTokenDTO addDTO) {
        SysStaff sysStaff = sysStaffMapper.selectOne(new LambdaQueryWrapper<SysStaff>()
                .eq(SysStaff::getUsername, addDTO.getUsername())
                .select()
        );
        if (addDTO.getExpireTime().isBefore(LocalDateTime.now())) {
            throw new ServiceException("设置的时间" + addDTO.getUsername() + "小于当前时间，请修改后提交");
        }
        if (sysStaff == null) {
            throw new ServiceException("该账号【" + addDTO.getUsername() + "】不存在，请修改后提交");
        }
        String token = AuthUtils.generateToken(sysStaff.getId(), 1, sysStaff.getTenantId(), true);
        SysToken sysToken = new SysToken();
        sysToken.setUsername(addDTO.getUsername());
        sysToken.setExpireTime(addDTO.getExpireTime());
        sysToken.setRemark(addDTO.getRemark());
        sysToken.setIsActive(addDTO.getIsActive());
        sysToken.setAccessToken(token);
        sysToken.setUpdateBy(AuthUtils.getCurrentUserId());
        sysToken.setCreateBy(AuthUtils.getCurrentUserId());
        sysTokenMapper.insert(sysToken);
    }

    @Override
    public SysToken selectById(Long id) {
        return sysTokenMapper.selectById(id);
    }

    @Override
    public int updateDeleted(Long id) {
        SysToken sysToken = new SysToken();
        sysToken.setId(id);
        sysToken.setDeleted(DbConstants.DEFAULT_YES);
        return sysTokenMapper.updateById(sysToken);
    }

    @Override
    public SysTokenVO selectVoById(Long id) {
        return sysTokenMapper.selectVOById(id);
    }

    @Override
    public void updateEntityById(SysToken sysToken) {
        sysToken.setUpdateBy(AuthUtils.getCurrentUserId());
        sysToken.setUpdateTime(new Date());
        updateById(sysToken);
    }
}
